const proxy = require('http-proxy-middleware');
const request = require('request');
const localIP = 'http://192.168.0.108:3344';
let accessToken = 'AAA';
const options = {
    method: 'post',
    url: `${localIP}/api/TokenAuth/Authenticate`,
    body: JSON.stringify({
        TenancyName: 'qr',
        usernameOrEmailAddress: 'demo',
        password: 'abc'
    }),
    headers: {
        'Content-Type': 'application/json; charset=utf-8'
    }
};
request(options, (err, res, body) => {
    if(err)
        console.log(err);
    else
        accessToken = JSON.parse(body).result.accessToken;
});

module.exports = {
    localIP,
    getAccessToken: () => `Bearer ${accessToken}`,
    localtest: proxy({
        target: localIP,
        changeOrigin: true,
        pathRewrite: {
            '^/parts': '',
        },
        onProxyReq(proxyReq, req, res) {
            proxyReq.setHeader('Authorization', `Bearer ${accessToken}`);
        }
    })
};
